package com.wc._16届国特冲刺营._10图论_LCA最近公共祖先.最近公共祖先LCA查询;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/6/1 11:56
 * @description
 * https://www.lanqiao.cn/courses/51805/learning/?id=4072924&compatibility=false
 */
public class Main {
    /**
     * tarjan 算法 模板
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = N << 1, K = 20;
    static int[] h = new int[N], e = new int[M], ne = new int[M];
    static int[][] f = new int[N][K];
    static int[] dpt = new int[N];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            add(a, b);
            add(b, a);
        }
        dfs (1, 0);
        m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt();
            out.println(lca(a, b));
        }
        out.flush();
    }

    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void dfs (int u, int fa) {
        f[u][0] = fa;
        dpt[u] = dpt[fa] + 1;
        for (int i = 1; i <= 17; i++) {
            f[u][i] = f[f[u][i - 1]][i - 1];
        }

        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == fa) continue;
            dfs(j, u);
        }
    }

    static int lca(int a, int b) {
        if (dpt[a] < dpt[b]) return lca(b, a);
        // 先跳到同一层
        for (int i = 17; i >= 0; i--) {
            if (dpt[f[a][i]] >= dpt[b]) a = f[a][i];
        }
        // 如果说 a = b 了, 就说明本来就是了
        if (a == b) return a;
        // 同时往上跳
        for (int i = 17; i >= 0; i--) {
            if (f[a][i] != f[b][i]) {
                a = f[a][i];
                b = f[b][i];
            }
        }
        return f[a][0];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

